package com.filebackup.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.filebackup.dao.LocalFileDao;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFileVo;
import com.filebackup.vo.impl.LocalFolderVo;
import com.filebackup.vo.impl.TestVo;

public class LocalFileDaoImpl implements LocalFileDao {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public boolean addVo(Vo vo) {
		System.out.println("dao success");
		
		this.getSessionFactory().getCurrentSession().save(vo);
		return false;
	}

	public boolean deleteVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().delete(vo);
		return false;
	}

	public List<Vo> findAll() {
		System.out.println("FIND ALL SUCCESS");
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFileVo");
		List<Vo> voList = query.list();
		return voList;
	}

	public Vo findByPK(Object pk) {
		Vo vo = (Vo) this.getSessionFactory().getCurrentSession().get(LocalFileVo.class, (Integer)pk);
		return vo;
	}

	public boolean updateVo(Vo vo) {
		System.out.println(((LocalFileVo)vo).getId());
		System.out.println(this.getSessionFactory().getCurrentSession());
		this.getSessionFactory().getCurrentSession().update(vo);
		System.out.println("a");
		return false;
	}

	public List<Vo> findByFolder(int id) {
		// TODO Auto-generated method stub
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFileVo where folderId=:folderId").setInteger("folderId", id);
		List<Vo> voList = query.list();
		return voList;
	}

	public List<Vo> findRootFile(int id) {
		// TODO Auto-generated method stub
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFileVo where ownerId=:ownerId and folderId=:folderId").setInteger("ownerId", id).setInteger("folderId", 0);
		List<Vo> voList = query.list();
		return voList;
	}

	public int findIdByVo(LocalFileVo vo) {
		// TODO Auto-generated method stub
		LocalFileVo lvo=(LocalFileVo)this.getSessionFactory().getCurrentSession().createQuery("from LocalFileVo where fileName=:fileName and ownerId=:ownerId and folderId=:folderId").setString("fileName", vo.getFileName()).setInteger("ownerId", vo.getOwnerId()).setInteger("folderId", vo.getFolderId()).uniqueResult();
		return lvo.getId();
	}

	public List<Vo> findByOwner(int id) {
		// TODO Auto-generated method stub
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFileVo where ownerId=:ownerId").setInteger("ownerId", id);
		List<Vo> voList = query.list();
		return voList;
	}
}
